﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using wnxd.javascript;

namespace Hidistro.UI.Web.API
{
    /// <summary>
    /// bz 的摘要说明
    /// </summary>
    public class bz : Page
    {

        /// <summary>
        /// 发短信
        /// </summary>
        /// <returns></returns>
        protected json HttpPost()
        {
            bool bflag = false;
            json json = new json(new { error = 1 });

            if (Session["SubmitTime"] == null)
            {
                Session["SubmitTime"] = DateTime.Now;
                bflag = true;
            }
            else
            {
                DateTime DtSubmiTime = (DateTime)Session["SubmitTime"];
                DtSubmiTime = DtSubmiTime.AddSeconds(60);
                if (DateTime.Now > DtSubmiTime)
                {
                    Session["SubmitTime"] = DateTime.Now;
                    bflag = true;
                }
                else json["time"] = (DtSubmiTime - DateTime.Now).TotalSeconds;
            }
            if (bflag == true)
            {
                StringBuilder strsms = new StringBuilder();
                strsms.Append("http://sdk2.entinfo.cn:8061/mdsmssend.ashx?sn=SDK-BBX-010-22946&pwd=3EDAE9A0C0352CC5835A23C7BC9C7902&mobile=");

                string strPhone = this.Request.Form["phone"].ToString();
                Regex reg = new Regex(@"1\d{10}");
                if (!reg.IsMatch(strPhone))
                {
                    Session.Remove("SubmitTime");
                    json["error"] = -1;
                }
                else
                {
                    strsms.Append(strPhone);
                    strsms.Append("&content=");

                    System.Random Random = new System.Random();
                    int iRes = Random.Next(1001, 9999);

                    string strContent = GetStrContent(iRes);
                    strsms.Append(strContent);
                    WebClient wc = new WebClient();
                    byte[] bResponse = wc.DownloadData(strsms.ToString());


                    this.Session["iRes"] = iRes;
                    this.Session["phone"] = strPhone;

                    long strResponse = long.Parse(Encoding.ASCII.GetString(bResponse));
                    if (strResponse > 0)
                    {
                        json["error"] = 0;
                    }
                    else
                    {
                        json["error"] = strResponse;
                    }
                }
            }
            return json;
        }

        public json mx()
        {
            json json = new json();
            if (Session["phone"] == null)
            {
                json["mes"] = "0";//请先获取验证码
            }
            else
            {
                if ((int)this.Session["iRes"] == Convert.ToInt32(this.Request.Form["yzm"]))
                {
                    json["mes"] = "1";//验证成功
                }
                else
                {
                    json["mes"] = "-1";//验证码错误
                }
            }
            return json;
        }


        string GetStrContent(int iRes)
        {
            string strContent = "请在页面输入校验码：";
            strContent += iRes.ToString();
            strContent += "【橘色商城】";

            string strurlutf8 = StringTourlUtf8(strContent);
            return strurlutf8;

        }
        public static string StringTourlUtf8(string s)
        {

            string msg = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(s));
            string encode = HttpUtility.UrlEncode(msg, System.Text.Encoding.UTF8).ToLower();

            return encode;
        }


        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }
    }
}